part of my_base_lib;

enum Platforms { android, ios, macOs, windows, linux, fuchsia, unknown }

class CommonUtils {

  //获取屏幕宽度
  static double getScreenWidth(BuildContext context) {
    return MediaQuery.of(context).size.width;
  }

  //获取屏幕高度
  static double getScreenHeight(BuildContext context) {
    return MediaQuery.of(context).size.height;
  }

  //获取屏幕顶部状态栏的高度
  static double getStatusBarHeight(BuildContext context) {
    return MediaQuery.of(context).padding.top;
  }

  //判断平台类型
  static Platforms getPlatform() {
    if (Platform.isAndroid) {
      return Platforms.android;
    } else if (Platform.isIOS) {
      return Platforms.ios;
    } else if (Platform.isMacOS) {
      return Platforms.macOs;
    } else if (Platform.isWindows) {
      return Platforms.windows;
    } else if (Platform.isLinux) {
      return Platforms.linux;
    } else if (Platform.isFuchsia) {
      return Platforms.fuchsia;
    } else {
      return Platforms.unknown;
    }
  }

  static bool isNotEmpty(String? string) {
    return string != null && string.isNotEmpty;
  }

  static bool isEmpty(String? string) {
    bool b1 = string == null || string.isEmpty;
    return b1;
  }

  static bool isValidEmail(String? email) {
    if (!isNotEmpty(email)) {
      return false;
    }
    // 定义email的正则表达式模式
    final RegExp emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
    // 使用正则表达式来检查email格式
    return emailRegex.hasMatch(email!);
  }

  //检测密码格式 由8到14位数字和密码组成，且同时至少包含一个数字、一个大写字母和一个小写字母
  static bool isValidPassword(String? password) {
    if (!isNotEmpty(password)) {
      return false;
    }
    final RegExp passwordRegex =
        RegExp(r'^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,14}$');
    return passwordRegex.hasMatch(password!);
  }


  //md5
  static String getMd5String(String string) {
    var bytes = utf8.encode(string); // 将输入字符串编码为字节
    var digest = md5.convert(bytes); // 计算 MD5 哈希值
    return digest.toString();
  }

  static void printString(String? string) {
    if (isEmpty(string)) {
      return;
    }

    for (int i = 0; i < string!.length; i += 600) {
      print('\n');
      print(string.substring(
          i, i + 600 > string!.length ? string!.length : i + 600));
    }
    print('\n');
  }

}
